package com.bite.mybatis.demo.mapper;

import com.bite.mybatis.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    //查询所有的用户信息
//    @Select("select id, username, password, age, gender, phone, " +
//            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info")
//    @Results(id = "BaseMap", value = {
//            @Result(column = "delete_flag", property = "deleteFlag"),
//            @Result(column = "create_time", property = "createTime"),
//            @Result(column = "update_time", property = "updateTime"),
//    })
    @Select("select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info")
    List<UserInfo> selectAll();

//    @Select("select * from user_info")
//    UserInfo selectAll2();

//    @ResultMap("BaseMap")
    @Select("select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info where id= #{aa}")
    UserInfo selectUserById(Integer id);

    //非唯一主键查询, 返回结果建议使用List
//    @Select("select * from user_info where age= #{age} and gender = #{gender}")  //1. 正确的写法  推荐写法
//    @Select("select * from user_info where age= #{age1} and gender = #{gender}")  // 参数绑定异常
//    @Select("select * from user_info where age= #{param2} and gender = #{param1}")  //2. 可以正确执行, 但是不推荐
    @Select("select * from user_info where age= #{age} and gender = #{gender}") //3.参数重命名  推荐写法, 参数和注解名称保持一致
    List<UserInfo> selectUserByAgeAndGender(@Param("gender") Integer gender, @Param("age") Integer age);

    //传递对象
    @Insert("insert into user_info (username, `password`, age, gender) " +
            "values (#{username}, #{password}, #{age}, #{gender})")
    Integer insertUser(UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, `password`, age, gender) " +
            "values (#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender})")
    Integer insertUserByParam(@Param("userInfo") UserInfo userInfo);

    //删除数据
    @Delete("delete from user_info where id =#{id}")
    Integer delete(Integer id);

    //修改数据
    @Update("update user_info set password = #{password} where id =#{id}")
    Integer update(Integer id, String password);

//    @Update("update user_info set password = #{password} where id =#{id}")
//    Integer update(UserInfo userInfo);

    //# 和 $ 区别
    @Select("select * from user_info where id= ${id}")
    UserInfo selectUserById1(Integer id);


//    @Select("select * from user_info where username= '${name}'")
    @Select("select * from user_info where username= '${name}' and password = '${password}'")
    List<UserInfo> selectUserByName(String name, String password);

    //排序功能
    @Select("select * from user_info order by id ${order}")
    List<UserInfo> selectUserByOrder(String order);

//    @Select("select * from user_info where username like '%${name}%'")
    @Select("select * from user_info where username like CONCAT('%',#{name},'%')")
    List<UserInfo> selectUserByLike(String name);

    @Insert("<script>" +
            "insert into user_info" +
            " <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
            " <if test=\"username!=null\"> username ,</if>" +
            " <if test=\"password!=null\"> password ,</if>" +
            " <if test=\"age!=null\"> age ,</if>" +
            " <if test=\"gender!=null\"> gender ,</if>" +
            " </trim> values" +
            " <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
            " <if test=\"username!=null\"> #{username},</if>" +
            " <if test=\"password!=null\"> #{password},</if>" +
            " <if test=\"age!=null\"> #{age},</if>" +
            " <if test=\"gender!=null\"> #{gender},</if>" +
            " </trim>"+
            "</script>")
    Integer insertUserByCondition(UserInfo userInfo);

}
